//![[CC
import IRewardedVideoPlugin from "../GMDAd/IRewardedVideoPlugin";
import { gcfg } from "../../gcfg";
import QWEventDispatcher from "../GMDBase/QWEventDispatcher";
import RewardedVideoEvts from "../GMDAd/RewardedVideoEvts";
import QWEvent from "../GMDBase/QWEvent";
import { PLATFORM_TAG, PlatformTags } from "../GMDBase/platform";
import { SDataVideoAD } from "../GMDAd/SDataVideoAD"; 
import { MD } from "../GMDBase/MD";
import mainLoop from "../GMDBase/mainLoop";
//!]]
let g_ucRewardedVideoPLInst;//UC视频插件实例


//判断当前环境是否支持UC视频
function SupportUCVideo():boolean
{
    return PLATFORM_TAG==PlatformTags.UC&&
        window["uc"]&&
        window["uc"].createRewardVideoAd
}



//微信视频上下文
class UCVideoContext
{
    //构造函数 分享口编号
    constructor(adUnitId:string)
    {
        if(!SupportUCVideo())
        {
            console.error("ucv pl 未初始化视频插件，需要在小游戏环境执行！")
            return
        }

        //var initParam = { adUnitId:adUnitId }
        console.log("ucv pl 初始化视频插件")


        //主循环
        mainLoop.startLoop(
            ()=>{
                if(this.mNeedReload) 
                {
                    this.mNeedReload = false
                
                    this.VidioAd.load()
                }
                return true
            }
        )
        
        //创建广告对象
        this.VidioAd = uc.createRewardVideoAd()

        //挂载广告关闭事件
        this.VidioAd.onClose((res) => {
            this.NeedShow = false
            this.Showing = false 
            
            var isEnded = res&&res.isEnded//是否观看完成
            console.error("ucv pl ========================视频观看结束"+isEnded) 
            var evtName = isEnded?RewardedVideoEvts.EVT_GIVE_REWARDED:RewardedVideoEvts.EVT_CLOSE

            var evt = new QWEvent(evtName) 
            g_ucRewardedVideoPLInst.Events.dispatchEvent(evt)     

            this.mNeedReload = true
       })

        //激励视频加载成功
        this.VidioAd.onLoad(() => {
            console.log("ucv pl onLoad")   
            MD.WXRewardedVideoST = 3
            this.IsLoadok = true

            if(this.NeedShow&&!this.Showing)
            { 
                MD.WXRewardedVideoST = 4
                console.log("ucv pl 视频加载成功，开始显示")
                this.show(this.mChannel)
            }else
            {
                MD.WXRewardedVideoST = 5
                console.log("ucv pl 视频加载成功，暂不显示")   
            }
        })

        //激励视频加载失败
        this.VidioAd.onError(err => {
            console.log("ucv pl onError")   
            this.IsLoadok = false
            this.NeedShow = false
            this.mNeedReload = true
            MD.WXRewardedVideoST = 6
            console.error("ucv pl 拉取视频失败！",err)
             

            var evt = new QWEvent(RewardedVideoEvts.EVT_NOT_READY) 
            g_ucRewardedVideoPLInst.Events.dispatchEvent(evt)
        })

        //console.log("ucv pl init load")   
        //this.VidioAd.load()
    }

    show(channel:number)
    {
        this.mChannel = channel

        if (!this.IsLoadok)//没有装载ok
        {
            console.log("ucv pl显示视频，没有加载成功，发起装载")
            MD.WXRewardedVideoST = 0
            this.NeedShow = true
            this.Showing = false

            this.mNeedReload = true

            return
        }

        console.log("ucv pl 显示广告")

         MD.WXRewardedVideoST = 1
         //显示广告
         this.NeedShow = true
         this.Showing = true
         this.VidioAd.show()
         .then()
         .catch(err => {
             console.error("ucv show err",err)
            this.NeedShow = false
            this.Showing = false 
            this.mNeedReload = true
        });
         
    }

    IsLoadok:boolean = false

    NeedShow:boolean = false

    //视频广告对象
    VidioAd:any
    Showing:boolean = false
    mChannel:number
    mNeedReload:boolean = false
}





//微信奖励视频实现
/*[*/export default/*]*/ class ucRewardedVideo implements IRewardedVideoPlugin
{
    constructor()
    {
        this.Events = new QWEventDispatcher()
    }


    public go()
    { 
        console.log("ucv go")
        g_ucRewardedVideoPLInst = this

        var adUnitId  = "single"
        var adObj = new UCVideoContext(adUnitId)
        this.mVidioAd[adUnitId] = adObj
    }

    

    //显示视频广告
    public show(channel:number)
    {
        if(!SupportUCVideo())//环境不支持
        {
            console.error("ucv show 环境不支持")
            var evt = new QWEvent(RewardedVideoEvts.EVT_NOT_READY) 
            this.Events.dispatchEvent(evt)
            return
        }
 
        console.log("ucv show #1")
        var adObj:UCVideoContext = this.mVidioAd["single"] 
        adObj.show(channel)
    }

  
    

    //添加事件监听
    public addEventListener(type: string, listener, owner = null) {
        this.Events.addEventListener(type, listener, owner)
    }

    //视频对象
    mVidioAd:any = {} 
    Events:QWEventDispatcher = null  
}